package com.xuqi.menucontrol;

import java.util.Stack;
import android.app.Activity;
import android.util.Log;

public class ScreenInfoStack {

	private static Stack<Activity> activityStack = new Stack<Activity>();
	private static ScreenInfoStack instance = new ScreenInfoStack();;
	
	private ScreenInfoStack(){
	}
	
	public static ScreenInfoStack getScreenInfoStack(){
		return instance;
	}
		
	public boolean HasActivity(Class cls)
	{
		if(activityStack == null || activityStack.empty())
			return false;
		
		 for (Activity i : activityStack)
		 {
			 if(i.getClass().equals(cls))
				return true;
		 }
		 return false;
	}
		
	
	public void popActivity(){   
		if(activityStack == null || activityStack.empty())
			return;
        Activity activity=activityStack.pop();  
        if(activity!=null){   
            activity.finish();   
        }   
    }  
	
	public Activity popActivityNoFinish(){   
		if(activityStack == null||activityStack.empty())
			return null;
		
        return activityStack.pop();           
    }
	
    public Activity currentActivity(){  
    	if(activityStack == null || activityStack.empty())
    		return null;
        Activity activity=activityStack.lastElement();   
        return activity;   
    }   
    
    public void pushActivity(Activity activity){ 
    	if(activity == null || activityStack == null)
    		return;
        activityStack.push(activity);   
    }   
   
    public void popAllActivityUntilOne(Class cls){   
    	if(activityStack == null)
			return;
    	
        while(false == activityStack.empty()){   
            Activity activity=currentActivity();   
            if(activity==null){   
                break;   
            }   
            if(activity.getClass().equals(cls) ){ 
            	popActivityNoFinish(); //but not finish
                break;   
            }   
            popActivity(); //finished  
        }   
    }   
   
    public void popAllActivity(){
       	while(activityStack != null && false == activityStack.empty()){   
    		popActivity();            
        }   
    }
    
    public void LogoutStack()
    {
    	Log.i("MenuControl","-----Screen Info Stack Begin----------------");
    	if(activityStack == null || activityStack.empty()){    		
    		Log.i("MenuControl","-----Screen Info Stack End-----------------");
    		return;
    	}			
    	
		 for (Activity i : activityStack)
		 {
			 Log.i("MenuControl","--" + i.getClass().toString());
		 }
		 Log.i("MenuControl","-----Screen Info Stack End-----------------");
    }
} 

